/*
 * Copyright 2025 NXP
 * NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
 * accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
 * activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
 * comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
 * terms, then you may not retain, install, activate or otherwise use the software.
 */

#include "events_init.h"
#include <stdio.h>
#include "lvgl.h"
#include "app_lvglDeal.h"
#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif

static void screen_main_btn_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_wifi, guider_ui.screen_wifi_del, &guider_ui.screen_main_del, setup_scr_screen_wifi, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        userLvglPar.screenIndex = SCREEN_WIFI;
        break;
    }
    default:
        break;
    }
}

static void screen_main_btn_2_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_devinfo, guider_ui.screen_devinfo_del, &guider_ui.screen_main_del, setup_scr_screen_devinfo, LV_SCR_LOAD_ANIM_NONE, 200, 20, false, false);
        userLvglPar.screenIndex = SCREEN_DEVINFO;
        break;
    }
    default:
        break;
    }
}

static void screen_main_btn_3_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_del, &guider_ui.screen_main_del, setup_scr_screen_nodeinfo, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        userLvglPar.screenIndex = SCREEN_NODEINFO;
        break;
    }
    default:
        break;
    }
}

void events_init_screen_main(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_main_btn_1, screen_main_btn_1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_main_btn_2, screen_main_btn_2_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_main_btn_3, screen_main_btn_3_event_handler, LV_EVENT_ALL, ui);
}

static void screen_devinfo_btn_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_main, guider_ui.screen_main_del, &guider_ui.screen_devinfo_del, setup_scr_screen_main, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        userLvglPar.screenIndex = SCREEN_MAIN;
        break;
    }
    default:
        break;
    }
}

void events_init_screen_devinfo(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_devinfo_btn_1, screen_devinfo_btn_1_event_handler, LV_EVENT_ALL, ui);
}
void clear_label_all()
{
    lv_obj_t *conts[] = {
        guider_ui.screen_nodeinfo_cont_1,
        guider_ui.screen_nodeinfo_cont_2,
        guider_ui.screen_nodeinfo_cont_3,
        guider_ui.screen_nodeinfo_cont_4,
        guider_ui.screen_nodeinfo_cont_5,
        guider_ui.screen_nodeinfo_cont_6};
    char *dataBuf[][6] = {
        {"", "", "", "", "", ""},
        {"", "", "", "", "", ""},
        {"", "", "", "", "", ""},
        {"", "", "", "", "", ""},
        {"", "", "", "", "", ""},
        {"", "", "", "", "", ""}};
    for (int i = 0; i < 6; i++)
    {
        LVGL_labels[i][0] = create_label_info(conts[i], LVGL_labels[i][0], dataBuf[i][0], &lv_font_SourceHanSerifSC_Regular_26, 4, 4, 32, 27, 1);     // 序号
        LVGL_labels[i][1] = create_label_info(conts[i], LVGL_labels[i][1], dataBuf[i][1], &lv_font_SourceHanSerifSC_Regular_26, 34, 27, 71, 27, 1);   // 温度
        LVGL_labels[i][2] = create_label_info(conts[i], LVGL_labels[i][2], dataBuf[i][2], &lv_font_SourceHanSerifSC_Regular_26, 46, 55, 56, 26, 1);   // 湿度
        LVGL_labels[i][3] = create_label_info(conts[i], LVGL_labels[i][3], dataBuf[i][3], &lv_font_SourceHanSerifSC_Regular_16, 37, 4, 90, 16, 1);    // 设备号
        LVGL_labels[i][4] = create_label_info(conts[i], LVGL_labels[i][4], dataBuf[i][4], &lv_font_SourceHanSerifSC_Regular_16, 40, 95, 64, 18, 1);   // 设备状态
        LVGL_labels[i][5] = create_label_info(conts[i], LVGL_labels[i][5], dataBuf[i][5], &lv_font_SourceHanSerifSC_Regular_12, 14, 120, 129, 14, 1); // 节点时间
    }
}
static void screen_nodeinfo_btn_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        // clear_label_all();
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_main, guider_ui.screen_main_del, &guider_ui.screen_nodeinfo_del, setup_scr_screen_main, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        lv_obj_add_flag(guider_ui.screen_nodeinfo_btn_3, LV_OBJ_FLAG_HIDDEN);
        lv_label_set_text(guider_ui.screen_nodeinfo_btn_2_label, "NEXT");
        userLvglPar.screenIndex = SCREEN_MAIN;
        break;
    }
    default:
        break;
    }
}
static void screen_nodeinfo_btn_2_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        switch (userLvglPar.screenIndex)
        {
        case SCREEN_NODEINFO:
            // clear_label_all();
            userLvglPar.screenIndex = SCREEN_NODEINFO_TWO;
            lv_obj_clear_flag(guider_ui.screen_nodeinfo_btn_3, LV_OBJ_FLAG_HIDDEN);
            break;
        case SCREEN_NODEINFO_TWO:
            // clear_label_all();
            userLvglPar.screenIndex = SCREEN_NODEINFO_THREE;
            lv_label_set_text(guider_ui.screen_nodeinfo_btn_2_label, "LAST");
            lv_obj_add_flag(guider_ui.screen_nodeinfo_btn_3, LV_OBJ_FLAG_HIDDEN);
            break;
        case SCREEN_NODEINFO_THREE:
            // clear_label_all();
            userLvglPar.screenIndex = SCREEN_NODEINFO_TWO;
            lv_label_set_text(guider_ui.screen_nodeinfo_btn_2_label, "NEXT");
            lv_obj_clear_flag(guider_ui.screen_nodeinfo_btn_3, LV_OBJ_FLAG_HIDDEN);
            break;
        }
        break;
    }
    default:
        break;
    }
}
static void screen_nodeinfo_btn_3_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        clear_label_all();
        userLvglPar.screenIndex = SCREEN_NODEINFO;
        lv_obj_add_flag(guider_ui.screen_nodeinfo_btn_3, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    default:
        break;
    }
}
void events_init_screen_nodeinfo(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_nodeinfo_btn_1, screen_nodeinfo_btn_1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_nodeinfo_btn_2, screen_nodeinfo_btn_2_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_nodeinfo_btn_3, screen_nodeinfo_btn_3_event_handler, LV_EVENT_ALL, ui);
}

static void screen_wifi_list_1_item0_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_wifipw, guider_ui.screen_wifipw_del, &guider_ui.screen_wifi_del, setup_scr_screen_wifipw, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, false);
        userLvglPar.screenIndex = SCREEN_WIFIPW;
        break;
    }
    default:
        break;
    }
}

static void screen_wifi_list_1_item1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_wifipw, guider_ui.screen_wifipw_del, &guider_ui.screen_wifi_del, setup_scr_screen_wifipw, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, false);
        userLvglPar.screenIndex = SCREEN_WIFIPW;
        break;
    }
    default:
        break;
    }
}

static void screen_wifi_list_1_item2_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_wifipw, guider_ui.screen_wifipw_del, &guider_ui.screen_wifi_del, setup_scr_screen_wifipw, LV_SCR_LOAD_ANIM_NONE, 200, 200, true, false);
        userLvglPar.screenIndex = SCREEN_WIFIPW;
        break;
    }
    default:
        break;
    }
}

static void screen_wifi_btn_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_main, guider_ui.screen_main_del, &guider_ui.screen_wifi_del, setup_scr_screen_main, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        userLvglPar.screenIndex = SCREEN_MAIN;
        break;
    }
    default:
        break;
    }
}

void events_init_screen_wifi(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_wifi_list_1_item0, screen_wifi_list_1_item0_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_wifi_list_1_item1, screen_wifi_list_1_item1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_wifi_list_1_item2, screen_wifi_list_1_item2_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_wifi_btn_1, screen_wifi_btn_1_event_handler, LV_EVENT_ALL, ui);
}

static void screen_wifipw_btn_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_wifi, guider_ui.screen_wifi_del, &guider_ui.screen_wifipw_del, setup_scr_screen_wifi, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        userLvglPar.screenIndex = SCREEN_WIFI;
        break;
    }
    default:
        break;
    }
}

static void screen_wifipw_btn_2_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_wifi, guider_ui.screen_wifi_del, &guider_ui.screen_wifipw_del, setup_scr_screen_wifi, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        userLvglPar.screenIndex = SCREEN_WIFI;
        break;
    }
    default:
        break;
    }
}

void events_init_screen_wifipw(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_wifipw_btn_1, screen_wifipw_btn_1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_wifipw_btn_2, screen_wifipw_btn_2_event_handler, LV_EVENT_ALL, ui);
}

void events_init(lv_ui *ui)
{
}
